package org.example.book.controller;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.book.constant.Constants;
import org.example.book.enums.ResultCode;
import org.example.book.model.*;
import org.example.book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/getBookListByPage")
    public Result getBookListByPage(PageRequest pageRequest, HttpSession session){
        log.info("查询翻页信息，pageRequest={}", pageRequest);
//        //用户登录校验
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if(userInfo == null||userInfo.getId()<=0||"".equals(userInfo.getUserName())){
//            //用户未登录
//            return Result.unlogin();
//        }
//        //校验成功
        if (pageRequest.getCurrentPage()<1||pageRequest.getPageSize()<0){
            return Result.fail("参数校验失败");
        }
        PageResult<BookInfo>bookInfoPageResult = null;
        try {
            bookInfoPageResult = bookService.selectBookInfoByPage(pageRequest);
            return Result.success(bookInfoPageResult);
        }catch (Exception e){
            log.error("查询翻页信息错误，e:{}",e);
            return Result.fail(e.getMessage());
        }
    }

    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo){
        log.info("接收到添加图书请求,bookInfo：",bookInfo);
        //参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())||!StringUtils.hasLength(bookInfo.getAuthor())
        ||bookInfo.getCount()<0||bookInfo.getPrice()==null
        ||!StringUtils.hasLength(bookInfo.getPublish())){
            return "参数校验失败，请检查输入";
        }
        Integer result = bookService.addBook(bookInfo);
        if (result <=0) {
            log.error("添加图书出错，bookInfo",bookInfo);
            return "添加图书出错，请联系管理员";
        }
        return "";
    }

    @RequestMapping("/queryBookInfoById")
    public BookInfo queryBookInfoById(Integer bookId){
        log.info("根据ID查询，当前ID",bookId);
        if(bookId<=0){
            return null;
        }
        return bookService.queryBookInfoById(bookId);

    }

    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("收到图书修改请求，bookInfo:",bookInfo);
        if(bookService.updateBook(bookInfo)>0){
            return "";
        }
        return "修改图书失败";
    }

    @RequestMapping(value = "/batchDelete",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids){
        log.info("接收到修改图书请求，ids:",ids);
        Integer result = bookService.batchDelete(ids);
        if (result <=0) {
            log.error("批量删除失败，请联系管理员");
            return "批量删除失败，请联系管理员";
        }
        return "";
    }


}

